// This File is part of the "nyaqt" Project
// 
// Copyright  2009 sometmes@gmail.com
// All rights reserved
// 
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.



using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace nyaqt
{
    class TextUtils
    {
        static Regex _endDeclareRegion = new Regex(@"^[^#\r\n]*#endregion[ \t]+declare[\w \t]*", RegexOptions.IgnoreCase | RegexOptions.Multiline);
        static Regex _goSeparator = new Regex(@"^[ \t]*go[ \t]*\r?\n", RegexOptions.IgnoreCase | RegexOptions.Multiline);

        public static void SplitScriptParts(out string declare, out string script, string text, int regionStart, int regionEnd)
        {
            MatchCollection declareMatches = _endDeclareRegion.Matches(text);
            if (declareMatches.Count > 0)
            {
                Match declareMatch = declareMatches[0]; // only one declare section allowed
                if (regionStart >= declareMatch.Index)
                {
                    declare = text.Substring(0, declareMatch.Index);
                    int start = Math.Max(declareMatch.Index + declareMatch.Length, regionStart);
                    script = text.Substring(start, regionEnd - start + 1);
                }
                else if (regionEnd < declareMatch.Index + declareMatch.Length)
                {
                    declare = "";
                    script = text.Substring(regionStart, regionEnd - regionStart + 1);
                }
                else
                {
                    int start = Math.Max(0, regionStart);
                    declare = text.Substring(start, declareMatch.Index - start);
                    start = declareMatch.Index + declareMatch.Length;
                    script = text.Substring(start, regionEnd - start + 1);
                }
            }
            else
            {
                declare = "";
                script = text.Substring(regionStart, regionEnd - regionStart + 1);
            }
        }
        public static string[] SplitBatches(string script)
        {
            return _goSeparator.Split(script);
        }
        public static Match MatchDeclareRegion(string text)
        {
            return _endDeclareRegion.Match(text);
        }
        public static string InsertBeforeEndDeclareRegion(string toInsert, string scriptWithRegionOrNot, string lineComment)
        {
            MatchCollection declareMatches = _endDeclareRegion.Matches(scriptWithRegionOrNot);
            if (declareMatches.Count > 0)
            {
                Match m = declareMatches[0];
                int iline = scriptWithRegionOrNot.LastIndexOf("\r\n", m.Index);
                return scriptWithRegionOrNot.Substring(0, iline) + "\r\n\r\n" + toInsert + "\r\n" +
                    scriptWithRegionOrNot.Substring(iline);
            }
            else
                return toInsert + "\r\n"+ lineComment + " #endregion Declare\r\n\r\n" + scriptWithRegionOrNot;
        }
    }
}
































